<?php

namespace app\mall\controller\admin;


use app\mall\enum\OrderStatusEnum;
use app\mall\logic\order\OrderAdminLogic as OrderLogic;
use app\mall\model\MallOrderEvaluate;
use suframe\thinkAdmin\traits\CURDController;
use suframe\thinkAdmin\ui\UITable;
use app\mall\model\MallOrder as MallOrderModel;
use think\facade\View;

/**
 * 订单管理
 */
class MallOrder extends Base
{
    use CURDController;

    protected $urlPre = '/mall/admin/mallOrder/';
    protected $status;

    /**
     * @param $action
     * @throws \Exception
     */
    private function CURLAllowActions($action)
    {
        $actions = ['index'];
        if (!in_array($action, $actions)) {
            throw new \Exception('action not found');
        }
    }

    protected function setNav($active = null)
    {
        if ($this->request->isAjax()) {
            return false;
        }
        parent::setNav();
        $this->status = $this->request->get('status', 0);
        $navs = [
            0 => ['全部', $this->urlABuild('index')]
        ];
        foreach (OrderStatusEnum::toZhArray() as $key => $item) {
            $navs[$key] = [$item, $this->urlABuild('index', ['status' => $key])];
        }

        $this->setAdminNavs($navs, $this->status);
    }

    private function beforeIndexRender($table)
    {
        $this->setNav();
    }

    private function getManageModel()
    {
        return MallOrderModel::class;
    }

    private function ajaxSearch()
    {
        $rs = $this->parseSearchWhere($this->getManageModel());
        $rs->append(['goods_title', 'store_name', 'user_nickname', 'status_text']);
        return json_return($rs);
    }

    /**
     * @param \suframe\form\Form $form
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \ReflectionException
     */
    private function getFormSetting($form)
    {
        $form->setRuleByClass(\app\mall\ui\form\MallOrderForm::class);
    }

    /**
     * @param UITable $table
     */
    private function getTableSetting($table)
    {
        $table->createByClass(\app\mall\ui\table\MallOrderTable::class);

        $configRole = [
            'type' => 'link',
            'blank' => true,
            'label' => '订单详细',
            'blankName' => '订单详细',
            'icon' => 'el-icon-document',
            'url' => $this->urlA('detail'),
            'vars' => ['id'],
        ];
        $table->setOps('detail', $configRole);
    }

    /**
     * 订单详细
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Exception
     */
    public function detail()
    {
        $id = $this->request->get('id');
        /** @var MallOrderModel $info */
        $info = MallOrderModel::find($id);
        $info->appendDetailFields(['admin_ops']);
        View::assign('data', $info->toJson());
        View::assign('kdUrl', $this->urlABuild('kd', ['id' => $info->id]));

        View::assign('urlCancel', $this->urlABuild('opCancel', ['id' => $info->id]));
        View::assign('urlRefund', $this->urlABuild('opRefund', ['id' => $info->id]));
        View::assign('urlSend', $this->urlABuild('opSend', ['id' => $info->id]));
        View::assign('urlComplete', $this->urlABuild('opComplete', ['id' => $info->id]));
        View::assign('urlEvaluate', $this->urlABuild('opEvaluate', ['id' => $info->id]));
        View::assign('urlUpload', config('thinkAdmin.upload_url'));
        View::assign('urlLogisticsCompany', url('/mall/admin/mallLogisticsCompany/getJson', ['id' => $info->id]));

        //订单评价
        $evaluates = '[]';
        if ($info->status_enum->canEvaluate()) {
            $evaluates = MallOrderEvaluate::where('sn', $info->sn)->append(['user'])->select()->toJson();
        }
        View::assign('evaluates', $evaluates);
        View::assign('manageEvaluateUrl', url('/mall/admin/mallOrderEvaluate', ['sn' => $info->sn]));
        return View::fetch('/mallOrder/detail');
    }

    /**
     * 快递查询
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Exception
     */
    public function kd()
    {
        $id = $this->request->get('id');
        /** @var MallOrderModel $info */
        $info = MallOrderModel::with('logistics')->find($id);
        View::assign('logistics_sn', $info->logistics_sn);
        View::assign('logistics_type', $info->logistics->getData('type'));
        return View::fetch('/mallOrder/kd');
    }

    /**
     * 取消订单
     * @return \think\response\Json
     * @throws \app\mall\exceptions\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Exception
     */
    public function opCancel()
    {
        $id = $this->requireParamInt('id');
        $reason = $this->request->post('reason', '');
        /** @var MallOrderModel $info */
        $info = MallOrderModel::find($id);
        $logic = new OrderLogic();
        $rs = $logic->cancel($info, $this->getAdminUser(), $reason);
        return $this->handleResponse($rs, '取消成功', '取消失败');
    }

    /**
     * 退款
     * @return \think\response\Json
     * @throws \app\mall\exceptions\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Exception
     */
    public function opRefund()
    {
        $id = $this->requireParamInt('id');
        $audit = $this->request->post('audit');
        $desc = $this->request->post('desc');
        /** @var MallOrderModel $info */
        $info = MallOrderModel::find($id);
        $logic = new OrderLogic();
        $rs = $logic->refund($audit == 'pass', $info, $this->getAdminUser(), $desc);
        return $this->handleResponse($rs, '处理成功', '处理失败');
    }

    /**
     * 发货
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Exception
     */
    public function opSend()
    {
        $id = $this->requireParamInt('id');
        $logistics_company_id = $this->request->post('logistics_company_id');
        $logistics_sn = $this->request->post('logistics_sn');
        /** @var MallOrderModel $info */
        $info = MallOrderModel::find($id);

        $logic = new OrderLogic();
        $rs = $logic->send($info, $this->getAdminUser(), $logistics_company_id, $logistics_sn);
        return $this->handleResponse($rs, '发货成功', '发货失败');
    }

    /**
     * 完成订单
     * @return \think\response\Json
     * @throws \app\mall\exceptions\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Exception
     */
    public function opComplete()
    {
        $id = $this->requireParamInt('id');
        $reason = $this->request->post('reason');
        /** @var MallOrderModel $info */
        $info = MallOrderModel::find($id);

        $logic = new OrderLogic();
        $rs = $logic->complete($info, $this->getAdminUser(), $reason);
        return $this->handleResponse($rs, '操作成功', '操作失败');
    }

    /**
     * 评价订单
     * @return \think\response\Json
     * @throws \app\mall\exceptions\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function opEvaluate()
    {
        $id = $this->requireParamInt('id');
        $rate = $this->request->post('rate');
        $content = $this->request->post('content');
        $images = $this->request->post('images');
        /** @var MallOrderModel $info */
        $info = MallOrderModel::find($id);

        $logic = new OrderLogic();
        $rs = $logic->evaluate($info, $this->getAdminUser(), $rate, $content, $images);
        return $this->handleResponse($rs, '操作成功', '操作失败');
    }
}